import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:maihealth/class/login/model/userModel.dart';
import 'package:maihealth/utils/httpUtils.dart';
import 'package:maihealth/utils/store.dart';


class loginProvide with ChangeNotifier{
  Map result = null;
  userModel model;
  bool isLogin = false;


  loginProvide(){
    getUserData().then((val){
      if (val!=null) {
          model = userModel.fromJson(jsonDecode(val));
          isLogin = true;
          print('已经登录');
          notifyListeners();
      }
    });
  }

//注销登录,清除用户信息缓存
  exitLogin(){
    model = null;
    storeUserDataWithSting(null);
  }

  
  //请求用户状态
  Future requestUserStatus(String tel) async{
     var data = await HttpUtils.post('/api/user/status/',params:{'username':tel,'send_sms':1});
      notifyListeners();
      return data;
  }

    //请求用户登录
  Future<userModel> requestLogin(String tel,String pwd) async{
        var response = await HttpUtils.post('/api/user/register_or_login/',
        params:{'username':tel,'device_type':1,'device_token':'','password':pwd});
         //存储用户信息
         String saveData = jsonEncode(response['data']);
         storeUserDataWithSting(saveData);
         print('存储的信息===${saveData}');
         model = userModel.fromJson(response['data']);
         isLogin = true;
         return model;
  }
} 